package com.example.demo.test;

import guru.nidi.graphviz.engine.Format;
import guru.nidi.graphviz.engine.Graphviz;
import guru.nidi.graphviz.engine.GraphvizCmdLineEngine;
import guru.nidi.graphviz.model.Factory;
import guru.nidi.graphviz.model.Graph;
import guru.nidi.graphviz.model.Node;

import java.io.File;
import java.io.IOException;

public class GraphvizExample {
    public static void main(String[] args) {
        // 使用命令行引擎
        Graphviz.useEngine(new GraphvizCmdLineEngine());

        Node a = Factory.node("A");
        Node b = Factory.node("B");
        Node c = Factory.node("C");

        // graph add node and edge
        Graph g = Factory.graph("example").directed()
                .with(a.link(b), a.link(c));  // Ensure linking between nodes

        // resource file
        File output = new File("graphTest.png");
        try {
            Graphviz.fromGraph(g).width(700).render(Format.PNG).toFile(output);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("Graph has been created and saved as graphTest.png");
    }
}
